var Workport = Workport || {};
// @Path("/Box")
Workport.BoxAPI = (function(){

	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'Box/';
	
	var urls = ["CheckUserToken", "{1}", "{1}" ];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++)
				r = r.replace("{"+i+"}", arguments[i]);
		}
		return r;
	}
	
	function sendGetRequest(url,callback,query,sync,data){
		if(sync){
			return sfw.Http.syncGet(url,callback,query);
		} else {
			return sfw.Http.get(url,callback,query);
		}
	}
	
	return {
		// @GET @Path("/CheckUserToken")
		// public BasicResponse CheckUserToken()
		checkToken:function(handler,request){
			return sendGetRequest(getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				code : request.code
			},request.sync );
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{FolderId}")
		// public BoxItemResponse GetBoxItems(@PathParam("FolderId") String folderId)
		getItems:function(handler,request){
			return sendGetRequest(getUrl(1,request.folderId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				} else {
					handler({ResponseCode:"WP2005"});
				}
			},{
				count:request.count,
				pid:request.pid,
				filter:request.filter
			},request.sync);
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/{ItemId}/EmbedUrl")
		// public EmbedUrlResponse GetEmbedUrl(@PathParam("ItemId") String itemId)
		getEmbedUrl:function(handler,request){
			return sendGetRequest(getUrl(2,request.itemId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},null,request.sync);
			function toViewModel(data){
				return data;
			}
		}
	}
})();
